package com.yunhe.authority.repository.office.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yunhe.authority.domain.authority.EmployeeUser;
import com.yunhe.authority.domain.authority.ProjectUser;
import com.yunhe.authority.domain.office.Office;
import com.yunhe.authority.model.housing.FilterCondition;
import com.yunhe.authority.model.housing.OfficeBrief;
import com.yunhe.authority.model.housing.OfficeQueryCondition;
import com.yunhe.common.util.CollectionUtil;
import com.yunhe.common.util.StringUtil;
import org.neo4j.ogm.model.Result;
import org.neo4j.ogm.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: OfficeDao
 * @Description:
 * @author: GongDaCheng
 * @Date: 15:18 2021/6/24
 */

@Repository
public class OfficeDao {

    @Autowired
    private Session session;

    public Iterable<OfficeBrief> getOfficeByCondition(FilterCondition filterCondition) {
        StringBuffer cql=new StringBuffer();
        //拼接基本语句
        cql.append("MATCH (rt:RealStation) -[:HAS_OFFICES]-> (o:Office) WHERE 1=1  ");
        //按房源状态查询
        if(!StringUtil.isEmpty(filterCondition.getOfficeStatus())){
            cql.append(" AND o.officeStatus='"+filterCondition.getOfficeStatus()+"'");
        }
        //按所属地区模糊查询
        if(!StringUtil.isEmpty(filterCondition.getArea())){
            cql.append(" AND o.region=~'.*"+filterCondition.getArea()+".*'");
        }
        //根据房源名称/入驻公司名称模糊查询
        if(!StringUtil.isEmpty(filterCondition.getKeyWords())){
            cql.append(" AND (o.officeTitle=~'.*"+filterCondition.getKeyWords()+".*'");
            cql.append(" or o.companyName=~'.*"+filterCondition.getKeyWords()+".*')");
        }
        //根据所选园区查询
        if(!CollectionUtil.isEmpty(filterCondition.getParkList())){
            cql.append(" AND ID(rt) IN "+filterCondition.getParkList()+"");
        }
        //拼接返回值和Limit部分
        int skip=(filterCondition.getPage()-1)*filterCondition.getSize();
        String queryEnd=" RETURN ID(rt) as regionParkId ,rt.abbreviationName as regionPark,ID(o) as officeId,o.officeTitle as officeTitle," +
                " o.region as region,o.officeArea as officeArea,o.officeStatus as officeStatus,o.companyName as settledCompanyName,o.settledCompanyId as  settledCompanyId,o.customerId as customerId, o.housingType as housingType" +
                " order by o.updateTime desc SKIP "+skip+" LIMIT "+filterCondition.getSize();
        String countEnd=" RETURN count(o)";


        //分页数据查询结果
        Result result = session.query(cql.toString() + queryEnd, Maps.newHashMap());
        //查询总数
        Integer total=session.queryForObject(Integer.class,cql.toString()+countEnd, Maps.newHashMap());

        //封装为分页类
        List<OfficeBrief> users = new ArrayList<>();
        if(result != null){
            for (Map<String, Object> map : result) {
                OfficeBrief projectUser = JSONObject.parseObject(JSONObject.toJSONString(map), OfficeBrief.class);
                users.add(projectUser);
            }
        }

        //封装为分页类
        Page<OfficeBrief> page = null;
        if(!CollectionUtil.isEmpty(users)){
            List<OfficeBrief> content= Lists.newArrayList(users);
            Pageable pageable= PageRequest.of(filterCondition.getPage()-1,filterCondition.getSize());
            page=new PageImpl<OfficeBrief>(content,pageable,total);
        }

        return page;
    }

    public Map<String, Object> findById(Long officeId) {
        StringBuffer cql=new StringBuffer();
        //拼接基本语句
        cql.append("MATCH (rt:RealStation) -[:HAS_OFFICES]-> (o:Office) WHERE 1=1  ");
        cql.append(" AND ID(o)="+officeId);
        String queryEnd=" RETURN ID(rt) as realStationId ,rt.abbreviationName as regionPark";
        Result result = session.query(cql.toString() + queryEnd, Maps.newHashMap());
        if(result != null){
            for (Map<String, Object> map : result) {
                return map;
            }
        }
        return null;
    }

    public Office searchById(Long officeId){
        String cql = "MATCH (o:Office) where ID(o) = " + officeId + " RETURN o LIMIT 1";
        Office office = session.queryForObject(Office.class, cql, Maps.newHashMap());
        return office;
    }

    public Office findByName(String officeName){
        String cql = "MATCH (o:Office) where o.officeTitle = '" + officeName + "' RETURN o LIMIT 1";
        Office office = session.queryForObject(Office.class, cql, Maps.newHashMap());
        return office;
    }

    public List<Office> findOfficesByRealStationIdAnOfiiceTitle(OfficeQueryCondition officeQueryCondition) {
        StringBuffer cql=new StringBuffer();
        //拼接基本语句
        cql.append("match(rt:RealStation)-[r:HAS_OFFICES]->(o:Office) WHERE 1=1  ");
        if(!CollectionUtil.isEmpty(officeQueryCondition.getRealStation())){
            cql.append(" AND ID(rt) IN"+officeQueryCondition.getRealStation());
        }
        if(!StringUtil.isEmpty(officeQueryCondition.getOfficeTitle())){
            cql.append(" AND o.officeTitle=~'.*"+officeQueryCondition.getOfficeTitle()+".*'");
        }
        String queryEnd=" RETURN ID(o) as id,o.officeTitle as officeTitle";
        //分页数据查询结果
        Result result = session.query(cql.toString() + queryEnd, Maps.newHashMap());
        List<Office> offices = new ArrayList<>();
        if(result != null){
            for (Map<String, Object> map : result) {
                Office office = JSONObject.parseObject(JSONObject.toJSONString(map), Office.class);
                offices.add(office);
            }
        }
        return offices;
    }
}
